﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _04.DicHashSet
{
    class DicHashSet<T> : IEnumerable<T> 
    {
        HashDictionary<T, T> hashDictionary;

        public DicHashSet()
        {
            this.hashDictionary = new HashDictionary<T, T>();
        }

        public int Count
        {
            get
            {
                return hashDictionary.Count;
            }
        }

        #region Methods

        public void Add(T item)
        {
            hashDictionary.Set(item, item);
        }

        public bool Contains(T item)
        {
            try
            {
                hashDictionary.Get(item);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public void Remove(T item)
        {
            hashDictionary.Remove(item);
        }

        public void Clear()
        {
            hashDictionary.Clear();
        }

        public void Union(DicHashSet<T> hashedSet)
        {
            foreach (var item in hashedSet)
            {
                if (!(this.Contains(item)))
                {
                    this.Add(item);
                }
            }
        }

        public void Intersect(DicHashSet<T> hashedSet)
        {
            HashDictionary<T, T> newtable = new HashDictionary<T, T>();
            foreach (var item in hashedSet)
            {
                if (this.Contains(item))
                {
                    newtable.Set(item, item);
                }
            }
            this.hashDictionary = newtable;
        }

        #endregion

        #region IEnumerable

        public IEnumerator<T> GetEnumerator()
        {
            return this.hashDictionary.Keys.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return hashDictionary.Keys.GetEnumerator();
        }

        #endregion
    }
}
